﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlashCards.Dto;
using System.Xml.Serialization;
using System.IO;
using FlashCards.SL.FlashCardRepeater.ViewModels.Commands;

namespace FlashCards.SL.FlashCardRepeater.ViewModels
{
    public class EndRepeatViewModel : BaseViewModel
    {
        private Repeat _repeat;

        private bool _isSavingRepeatCompleted;
        public bool IsSavingRepeatCompleted
        {
            get { return _isSavingRepeatCompleted; }
            set { _isSavingRepeatCompleted = value; OnPropertyChanged("IsSavingCompleted"); }
        }

        public bool IsSavingCompleted
        {
            get { return IsSavingRepeatCompleted; }
        }

        public event EventHandler<EndRepeatEventArgs> OnStartOverCompleted;

        public EndRepeatViewModel(Repeat repeat, FlashcardSet set, string endpointName)
        {
            InitializeCommands();
            _repeat = repeat;

            var proxy = new DataServices.SilverlightServiceClient(endpointName);

            proxy.SaveRepeatCompleted += proxy_SaveRepeatCompleted;
            //proxy.SaveSetLastMarksCompleted += proxy_UpdateExistingSetCompleted;

            var serializer = new XmlSerializer(typeof(Repeat));
            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, _repeat);

                proxy.SaveRepeatAsync(writer.ToString());
            }

            //serializer = new XmlSerializer(typeof(FlashcardSet));
            //using (var writer = new StringWriter())
            //{
            //    serializer.Serialize(writer, set);
            //    proxy.SaveSetLastMarksAsync(writer.ToString());
            //}
        }

        void proxy_SaveRepeatCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                IsSavingRepeatCompleted = true;
            }
        }

        #region COMMANDS

        public ICommand StartOverCommand { get; private set; }

        void InitializeCommands()
        {
            StartOverCommand = new Command(StartOver);
        }

        void StartOver()
        {
            if (OnStartOverCompleted != null)
                OnStartOverCompleted.Invoke(this, new EndRepeatEventArgs(_repeat, null));
        }

        #endregion
    }

    public class EndRepeatEventArgs : EventArgs
    {
        public Repeat Repeat { get; set; }
        public FlashcardSet Set { get; set; }

        public EndRepeatEventArgs(Repeat repeat, FlashcardSet set)
        {
            Repeat = repeat;
            Set = set;
        }
    }
}
